package whu.cn.whushare.ui.main.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.VolleyError;
import com.androidplus.util.ViewMapping;
import com.androidplus.util.ViewMappingUtil;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

import whu.cn.whushare.R;
import whu.cn.whushare.common.Constants;
import whu.cn.whushare.net.DownloadParams;
import whu.cn.whushare.net.Downloader;
import whu.cn.whushare.ui.common.BaseActivity;
import whu.cn.whushare.ui.common.LoadingHelper;
import whu.cn.whushare.ui.main.activity.adapter.SearchActivityAdapter;
import whu.cn.whushare.ui.main.activity.bean.SearchActivity;
import whu.cn.whushare.ui.main.known.QuestionDetailActivity;
import whu.cn.whushare.ui.main.known.adapter.SearchQuestionAdapter;
import whu.cn.whushare.ui.main.known.bean.SearchQuestion;
import whu.cn.whushare.util.SetOnClickUtil;
import whu.cn.whushare.util.Utils;

/**
 * Created by xiaoping on 2015/4/9.
 */
@ViewMapping(id=R.layout.activity_activity_list)
public class ActivityListActivity  extends BaseActivity {
    private LoadingHelper mLoadingHelper;

    private int mCurrentPage = 1;

    @ViewMapping(id = R.id.activity_list)
    private PullToRefreshListView mRefreshListView;

    private SearchActivityAdapter mAdapter;
    private List<SearchActivity> mActivityTitles = new ArrayList<>();
    private String mQuery;
    private String mActivityId;
    public static Intent getStartIntent(Context context, String activity) {
        Intent i = new Intent(context, ActivityListActivity.class);
        i.putExtra(Constants.INTENT_EXTRA_ACTIVITY, activity);
        i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        return i;
    }

    protected void requestSearchActivity(String query, int page, int pageSize) {
        mLoadingHelper.showLoadingView(false);
        Downloader.getInstance(this).getQueryActivitiesList(query, page, pageSize, this, page);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();

    }


    private void initView() {
        View root = ViewMappingUtil.mapView(this, this);
        getTopBar(root);
        SetOnClickUtil.registerOnClick(this);
        mTopBar.searchLayout.setVisibility(View.VISIBLE);
        initLoadingHelper();
        initListView();
        mTopBar.searchView.requestFocus();
        mTopBar.searchView.setSelected(true);
        mTopBar.searchTxt.setOnClickListener(this);
    }

    private void initListView() {
        mAdapter = new SearchActivityAdapter(this,mActivityTitles);
        mRefreshListView.setAdapter(mAdapter);
        mRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        mRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> listViewPullToRefreshBase) {
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> listViewPullToRefreshBase) {
                if (mActivityTitles.size() >= mTotal) {
                    Utils.setRefreshLabelToLast(ActivityListActivity.this, mRefreshListView);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mRefreshListView.onRefreshComplete();
                        }
                    }, 500);
                } else {
                    requestSearchActivity(mQuery, mCurrentPage + 1, 10);
                }
            }
        });
        mRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SearchActivity searchActivity = (SearchActivity)mAdapter.getItem(position - 1);
                mActivityId=searchActivity.activity.getActivityId();
                saveInvite();
            }
        });
    }
    private void saveInvite() {
        startActivityForResult(ActivityDetailActivity.getStartIntent(this, mActivityId),1);
    }
    private void initLoadingHelper() {
        mLoadingHelper = new LoadingHelper(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestSearchActivity(mQuery, mCurrentPage, 10);
            }
        });
        mLoadingHelper.onCreateView(LayoutInflater.from(this), mRefreshListView);
    }

    private void updateView(List<SearchActivity> searchActivities) {
        if (mCurrentPage == 1) {
            mActivityTitles.clear();
            Utils.resetRefreshLabel(this, mRefreshListView);
        }
        if (searchActivities.size() == 0) {
            toast("无记录");
        } else {
            mActivityTitles.addAll(searchActivities);
        }
    }

    @Override
    public <T> void onSuccess(DownloadParams.PARSING_TYPE type, T response, Object obj) {
        super.onSuccess(type, response, obj);
        mRefreshListView.onRefreshComplete();
        mLoadingHelper.showContentView();

        if (response != null) {
            mCurrentPage = (int) obj;

            updateView((List<SearchActivity>) response);
        } else {
            toast("无记录");
        }
    }

    @Override
    public void onFail(DownloadParams.PARSING_TYPE type, VolleyError error) {
        super.onFail(type, error);
        mLoadingHelper.showRetryView();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.searchTxt:
                mQuery = mTopBar.searchView.getQuery().toString();
                System.out.print(mQuery);
                if (TextUtils.isEmpty(mQuery)) {
                    toast(R.string.no_query);
                } else {
                    requestSearchActivity(mQuery, 1, 10);
                }
        }
    }
}

